package com.kongwg.java;

/**
 * 使用同步方法处理继承Thread类的方式中的线程安全问题
 * @author kongwg
 * @create 2020-12-05 15:11
 */
class Windows4 extends Thread{
    private static int ticket=100;

    @Override
    public void run() {
        while (true){
            show();
        }
    }

    private static synchronized void show() {
        if (ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "卖出的票号为" + ticket);
            ticket--;
        }
    }
}

public class WindowsTest4 {
    public static void main(String[] args) {
        Windows4 t1 = new Windows4();
        Windows4 t2 = new Windows4();
        Windows4 t3 = new Windows4();

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}
